package com.zch.action;

import java.util.List;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import com.base.log.annotation.ControllerLog;
import com.base.mobile.base.Md5;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.action.CoreController;
import com.zch.dao.model.ChatDept;
import com.zch.dao.model.ChatDeptExample;
import com.zch.service.ChatDeptService;

@RequestMapping("chatDept")
@Controller
@LoginFilter
public class ChatDeptController extends CoreController{

	@Autowired
	private ChatDeptService chatDeptService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows){
		PaginationSupport.byPage(page, rows);
		ChatDeptExample example = new ChatDeptExample();
		example.setOrderByClause("sort_order");
		List<ChatDept> list = chatDeptService.selectByExample(example);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ChatDept chatDept){
		if (StringUtils.isBlank(chatDept.getCode())){
			chatDept.setCode(chatDeptService.getCode());
			chatDept.setRoleCode("street");
			chatDept.setPassword(Md5.mD5(chatDept.getPassword()));
			chatDeptService.insert(chatDept);
		}else{
			if (StringUtils.isNotBlank(chatDept.getPassword())){
				chatDept.setPassword(Md5.mD5(chatDept.getPassword()));
			}
			chatDeptService.updateByPrimaryKeySelective(chatDept);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String code){
		ChatDept chatDept = chatDeptService.selectByPrimaryKey(code);
		return JSONUtils.serialize(chatDept);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			chatDeptService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/isAccountExist")
	@ResponseBody
	public String isAccountExist(String value){
		ChatDeptExample example = new ChatDeptExample();
		example.createCriteria().andAccountEqualTo(value);
		int cnt = chatDeptService.countByExample(example);
		if(cnt>0)
			return JSONUtils.serialize(new Ret(1));
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/isNameExist")
	@ResponseBody
	public String isNameExist(String value){
		ChatDeptExample example = new ChatDeptExample();
		example.createCriteria().andNameEqualTo(value);
		int cnt = chatDeptService.countByExample(example);
		if(cnt>0)
			return JSONUtils.serialize(new Ret(1));
		return JSONUtils.serialize(new Ret(0));
	}
}